package com.citrix.gs.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class HtmlEmailSender {
    
    public void sendHtmlEmail(String host, String port, 
            final String userName, final String password, String fromAddress, String toAddress,
            String subject, String message) throws Exception {
        
        // sets SMTP server properties
        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.port", port);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        
        // creates a new session with an authenticator
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(userName, password);
            }
        };
        
        Session session = Session.getInstance(properties, auth);
        
        try {
            // creates a new e-mail message
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(fromAddress));
            InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
            msg.setRecipients(Message.RecipientType.TO, toAddresses);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            // set plain text message
            msg.setContent(message, "text/html");
     
            // sends the e-mail
            Transport.send(msg);
        } catch(Exception e) {
            throw e;
        } finally {
        }
    }
    
    /**
     * Test the send html e-mail method
     *
     */
    public static void main(String[] args) {
        
        // SMTP server information
        String host = "sinpmail.citrite.net";
        String port = "25";
        String mailFrom = "svcacct_gsnkg@citrix.com";
        String username = "svcacct_gsnkg";
        String password = "7eyuchXx";
 
        // outgoing message information
        String mailTo = "yu.zhou@citrix.com";
        String subject = "Hello my friend";
 
        // message contains HTML markups
        String message = "<i>Greetings!</i><br>";
        message += "<b>Wish you a nice day!</b><br>";
        message += "<font color=red>Duke</font>";
        message += "<table border='0'>"
                + "<thead><th>Repository</th><th>Error Count</th></thead>"
                + "<tr><td>"
                + "<a href=\"http://www.baidu.com\">Baidu</a>"
                + "</td><td>ddd</td></tr>"
                + "</table>";
 
        HtmlEmailSender mailer = new HtmlEmailSender();
 
        try {
            mailer.sendHtmlEmail(host, port, username, password, mailFrom, mailTo,
                    subject, message);
            System.out.println("Email sent.");
        } catch (Exception ex) {
            System.out.println("Failed to sent email.");
            ex.printStackTrace();
        }
    }

}
